package com.panopset.io;

import java.awt.Window;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

import com.panopset.compat.Strings;
import com.panopset.nls.MsgProps;
import com.panopset.sf.FontManager;
import com.panopset.sf.FrameState;
import com.panopset.sf.Windoc;

/**
 * Application base directory, and document registry.
 * 
 * @author Karl Dinwiddie
 * 
 */
public final class AppBase {

	/**
	 * Application base directory.
	 */
	private final File bd;

	public File getBaseDirectory() {
		return bd;
	}

	/**
	 * 
	 * @param baseDir
	 *            Application base directory.
	 */
	public AppBase(final File baseDir) {
		bd = baseDir;
	}

	private Set<String> docsList;

	public Set<String> getDocsList() {
		if (docsList == null) {
			docsList = Collections.synchronizedSet(new TreeSet<String>());
			File f = getAppDocsFile();
			if (!f.canRead()) {
				return docsList;
			}
			try (FileReader fr = new FileReader(f);
					BufferedReader br = new BufferedReader(fr);) {
				String s = br.readLine();
				while (s != null) {
					docsList.add(s);
					s = br.readLine();
				}
			} catch (final IOException ex) {
				throw new RuntimeException(ex);
			}
		}
		return docsList;
	}

	/**
	 * Base directory/docs.txt.
	 */
	private File appDocsFile;

	/**
	 * 
	 * @return List of application documents in base directory/docs.txt.
	 */
	public File getAppDocsFile() {
		if (appDocsFile == null) {
			appDocsFile = new File(new Filez().combinePaths(bd, "docs.txt"));
		}
		return appDocsFile;
	}

	/**
	 * Application properties.
	 */
	private FileProps appProps;

	/**
	 * 
	 * @return Application properties.
	 */
	private FileProps getAppProps() {
		if (appProps == null) {
			appProps = new FileProps(new File(new Filez().combinePaths(bd,
					"app.properties")));
			FontManager.getInstance().setProps(appProps);
		}
		return appProps;
	}

	// just a mere distraction now but...
	// getAppProps().set(DFT_PARENT_DIR, getDefaultAppFileParentDir());

	/**
	 * Default app file parent directory.
	 */
	private String defaultAppFileParentDir;

	/**
	 * com.panopset.app.DFT_PARENT_DIR.
	 */
	private final String DFT_PARENT_DIR = "com.panopset.app.DFT_PARENT_dir";

	/**
	 * 
	 * @param value
	 *            New default application file parent directory.
	 */
	public void setDefaultAppFileParentDir(final String value) {
		defaultAppFileParentDir = value;
	}

	/**
	 * 
	 * @return Default app file parent dir.
	 */
	public String getDefaultAppFileParentDir() {
		if (defaultAppFileParentDir == null) {
			defaultAppFileParentDir = getAppProps().get(DFT_PARENT_DIR);
			if (!Strings.isPopulated(defaultAppFileParentDir)) {
				defaultAppFileParentDir = System.getProperty("user.home");
			}
		}
		return defaultAppFileParentDir;
	}

	public void saveas(final Window w) {
		FileProps fp = new FileProps(getNewFileFromUser(w));
		new FrameState(fp).save(w);

	}


	public File getOpenedFileFromUser(final Window parentFrame) {
		File rtn = null;
		JFileChooser chooser = new JFileChooser();
		chooser.setSelectedFile(new File(getDefaultAppFileParentDir()));
		// whatever tf chooser.setFileSelectionMode();
		chooser.setDialogTitle(new MsgProps(Windoc.SD_MSG_PROPS)
				.get("fileprompt"));
		chooser.setDialogType(JFileChooser.OPEN_DIALOG);
		if (chooser.showOpenDialog(parentFrame)

		== JFileChooser.APPROVE_OPTION) {
			rtn = chooser.getSelectedFile();
			setDefaultAppFileParentDir(new Filez().getCanonicalPath(rtn
					.getParentFile()));
		}

		return rtn;
	}

	public File getNewFileFromUser(final Window parentFrame) {
		File rtn = null;
		JFileChooser chooser = new JFileChooser();
		chooser.setSelectedFile(new File(getDefaultAppFileParentDir()));
		// whatever tf chooser.setFileSelectionMode();
		chooser.setDialogTitle(new MsgProps(Windoc.SD_MSG_PROPS)
				.get("fileprompt"));
		chooser.setDialogType(JFileChooser.SAVE_DIALOG);
		if (chooser.showOpenDialog(parentFrame)

		== JFileChooser.APPROVE_OPTION) {
			rtn = chooser.getSelectedFile();
			setDefaultAppFileParentDir(new Filez().getCanonicalPath(rtn
					.getParentFile()));
		}

		return rtn;
	}

	private final Log log = new Log();

	class Log {

		public void append(String msg) {
			System.out.println(msg);
		}

	}
}
